<template>
  <el-dialog :title="formValidate.id ? $t('productCenter.appConfig.bank.edit') : $t('productCenter.appConfig.bank.add')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('productCenter.appConfig.bank.sort')" prop="sort">
        <el-select v-model="formValidate.sort" :placeholder="$t('common.select')" :disabled="!formValidate.id" style="width: 100%">
          <el-option
            v-for="item in maxSort"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('productCenter.appConfig.bank.name')" prop="bankName">
        <el-input
          v-model="formValidate.bankName"
          clearable
          :placeholder="$t('productCenter.appConfig.bank.prompt.name')"
          :maxlength="100"
        />
      </el-form-item>
      <el-form-item :label="$t('productCenter.appConfig.bank.logo')" prop="file">
        <up-load-image
          ref="upload"
          :img-width="150"
          :img-height="150"
          :img-url="formValidate.file"
          @getUrl="getImgUrl"
        />
        {{ $t('productCenter.appConfig.bank.recommendedSize') }} 120px * 120px
      </el-form-item>
      <el-form-item :label="$t('productCenter.appConfig.bank.state')" prop="status">
        <el-select v-model="formValidate.status" :placeholder="$t('productCenter.appConfig.bank.prompt.state')" style="width: 100%">
          <el-option
            v-for="item in stateData"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addBank, updateBank } from '@/api/product'
import UpLoadImage from '@/components/product/up-load-image.vue'
export default {
  name: 'BankManagementDialog',
  components: {
    UpLoadImage
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    maxSort: {
      type: Number,
      default: 1
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    const validateFile = (rule, value, callback) => {
      if (!this.formValidate.file && !this.file) {
        callback(new Error(this.$t('productCenter.appConfig.bank.logo') + this.$t('common.null')))
      } else {
        callback()
      }
    }
    return {
      submitLoading: false,
      stateData: (this.$t('productCenter.appConfig.bank.status')).slice(1),
      file: null,
      formValidate: {
        id: null,
        sort: null,
        bankName: null,
        file: null,
        status: null
      },
      rules: {
        sort: [{ required: true, type: 'number', message: this.$t('productCenter.appConfig.bank.sort') + this.$t('common.null'), trigger: 'blur' }],
        bankName: [{ required: true, message: this.$t('productCenter.appConfig.bank.name') + this.$t('common.null'), trigger: 'blur' }],
        file: [{ required: true, validator: validateFile, trigger: 'change' }],
        status: [{ required: true, message: this.$t('productCenter.appConfig.bank.state') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    getImgUrl(file) {
      console.log(file)
      this.file = file
    },
    handleClose() {
      this.$emit('change-dialog', 'editDialog', false)
      this.$refs['formValidate'].clearValidate()
      this.$refs.upload.init()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = new FormData()
          data.append('sort', this.formValidate.sort)
          data.append('bankName', this.formValidate.bankName)
          if (this.file) {
            data.append('file', this.file)
          }
          data.append('status', this.formValidate.status)
          if (this.formValidate.id) {
            data.append('id', this.formValidate.id)
            updateBank(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('change-dialog', 'editDialog', false)
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addBank(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('change-dialog', 'editDialog', false)
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        }
      })
    }
  }
}
</script>

<style scoped>

</style>

